﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASIMLib;

namespace SalaryChannel.Web
{
    public class AMSender : IDisposable
    {

        private string AmServer;

        private string AmSenderID;

        private string AmSenderPwd;


        private string DefalutReceivers;


        /// <summary>
        /// 在配置文件中获取参数，完成初始化。
        /// </summary>
        public AMSender()
        {
            AmSenderID = System.Configuration.
                          ConfigurationSettings.AppSettings["AmSender"];
            AmSenderPwd = System.Configuration.
                       ConfigurationSettings.AppSettings["AmSenderPwd"];
            AmServer = System.Configuration.
                       ConfigurationSettings.AppSettings["AmServer"];
            DefalutReceivers = System.Configuration.
                       ConfigurationSettings.AppSettings["AmReceiver"];

            if (string.IsNullOrEmpty(AmSenderID)
                || string.IsNullOrEmpty(AmSenderPwd)
                || string.IsNullOrEmpty(AmServer))
            {
                throw new Exception("AMSender初始化失败，没有在配置文件中指定足够的信息");
            }

            im = new IMClass();

            im.Init(AmServer, AmSenderID, AmSenderPwd);
        }

        /// <summary>
        /// 指定参数，初始化
        /// </summary>
        /// <param name="Server"></param>
        /// <param name="Sender"></param>
        /// <param name="Pwd"></param>
        public AMSender(string Server, string Sender, string Pwd, string DefaultReceivers)
        {
            this.AmSenderID = Sender;
            this.AmServer = Server;
            this.AmSenderPwd = Pwd;
            this.DefalutReceivers = DefaultReceivers;
            if (string.IsNullOrEmpty(AmSenderID)
                || string.IsNullOrEmpty(AmSenderPwd)
                || string.IsNullOrEmpty(AmServer))
            {
                throw new Exception("AMSender初始化失败，没有指定足够的信息");
            }


            im = new IMClass();

            im.Init(AmServer, AmSenderID, AmSenderPwd);
        }

        IMClass im;

        /// <summary>
        /// 将信息发送到指定的接收人
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="Receivers">多个接收人用分号隔开,为空时发送给默认接收人</param>
        /// <param name="Attachment">若不发送附件，请指定为null</param>
        /// <returns></returns>

        public bool SendMsg(string subject, string body, string Receivers, string[] Attachments)
        {

            if (string.IsNullOrEmpty(body)
                && Attachments == null
                && Attachments.Length == 0)
            {
                throw new ArgumentNullException("消息为空,不发送");
            }

            subject = subject == null ? string.Empty : subject;

            if (string.IsNullOrEmpty(Receivers))
            {
                Receivers = DefalutReceivers;
            }
            Msg amMsg = new Msg
            {
                ContentType = "Text/Text",
                Subject = subject,
                Body = body
            };
            if (Attachments != null)
            {
                foreach (string file in Attachments)
                {

                    if (!System.IO.File.Exists(file))
                    {
                        throw new ArgumentException("指定的附件不存在；若不发送附件，请指定为null." + file);
                    }
                    amMsg.AddAttachment(file);
                }

            }
            int lastErrorCode = im.SendMsgEx(amMsg, Receivers);

            return lastErrorCode == 1;

        }




        #region IDisposable 成员

        public void Dispose()
        {
            this.AmSenderID = null;
            this.AmSenderPwd = null;
            this.AmServer = null;
            this.DefalutReceivers = null;
            im = null;
        }

        #endregion
    }
}
